package com.bw.paper.mvp.view.Fragment.news.Adapter;

import android.widget.ImageView;

import com.blankj.utilcode.util.TimeUtils;
import com.bw.paper.R;
import com.bw.paper.entity.NewsEntity;
import com.bw.paper.network.LoadImage;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;

import java.util.Date;

public class ContentNewsAdapter extends BaseMultiItemQuickAdapter<NewsEntity.Values, BaseViewHolder> {

    public ContentNewsAdapter(){
        //3.+版本时 addChildClickViewIds()
        addItemType(NewsEntity.NONECODE, R.layout.item_news4);
        addItemType(NewsEntity.IMG1CODE, R.layout.item_news1);
        addItemType(NewsEntity.IMG2CODE, R.layout.item_news3);
        addItemType(NewsEntity.IMG3CODE, R.layout.item_news2);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, NewsEntity.Values values) {
        //由于Android时间戳System.currentTimeMillis()获取后单位为毫秒值
        //上传服务器时值/1000,服务器获取的时间戳使用时*1000
        Date date = new Date(values.news_time*1000);
        String timeStr = TimeUtils.getFriendlyTimeSpanByNow(date);
        if (values.getItemType() == NewsEntity.NONECODE){
            baseViewHolder.setText(R.id.news4_title,values.news_title);
            baseViewHolder.setText(R.id.news4_author,values.news_author);
            if (values.news_time <= 0) {
                baseViewHolder.setText(R.id.news4_time, "很久以前");
                return;
            }
            baseViewHolder.setText(R.id.news4_time, timeStr);
            return;
        }
        if (values.getItemType() == NewsEntity.IMG1CODE){
            baseViewHolder.setText(R.id.news1_title,values.news_title);
            baseViewHolder.setText(R.id.news1_author,values.news_author);
            if (values.news_time <= 0) {
                baseViewHolder.setText(R.id.news1_time, "很久以前");
                return;
            }
            baseViewHolder.setText(R.id.news1_time, timeStr);
            ImageView img = baseViewHolder.getView(R.id.news1_img);
            LoadImage.loadMatchImg( values.getMedias().get(0).media_url,img);
            return;
        }
        if (values.getItemType() == NewsEntity.IMG2CODE){
            baseViewHolder.setText(R.id.news3_title,values.news_title);
            baseViewHolder.setText(R.id.news3_author,values.news_author);
            if (values.news_time <= 0) {
                baseViewHolder.setText(R.id.news3_time, "很久以前");
                return;
            }
            baseViewHolder.setText(R.id.news3_time, timeStr);
            ImageView img = baseViewHolder.getView(R.id.news3_img1);
            ImageView img1 = baseViewHolder.getView(R.id.news3_img2);
            LoadImage.loadMatchImg( values.getMedias().get(0).media_url,img);
            LoadImage.loadMatchImg( values.getMedias().get(1).media_url,img1);
            return;
        }
        if (values.getItemType() == NewsEntity.IMG3CODE){
            baseViewHolder.setText(R.id.news2_title,values.news_title);
            baseViewHolder.setText(R.id.news2_author,values.news_author);
            if (values.news_time <= 0) {
                baseViewHolder.setText(R.id.news2_time, "很久以前");
                return;
            }
            baseViewHolder.setText(R.id.news2_time, timeStr);
            ImageView img = baseViewHolder.getView(R.id.news2_img1);
            ImageView img1 = baseViewHolder.getView(R.id.news2_img2);
            ImageView img2 = baseViewHolder.getView(R.id.news2_img3);
            LoadImage.loadMatchImg( values.getMedias().get(0).media_url,img);
            LoadImage.loadMatchImg( values.getMedias().get(1).media_url,img1);
            LoadImage.loadMatchImg( values.getMedias().get(2).media_url,img2);
            return;
        }
    }
}